<?php
require("personClass.php"); 
class agent extends person	
{	

        public $agentFields = array(	
        "person_number" => "",
        "person_id" => "",
        "firstname" => "",
        "lastname" => "",
        "email" => "",
        "city" => "",
        "address" => "",
        "telephone" => "",
        "fax" => "",
        "cellphone" => "",
        "person_number" => "",
        "pass_word" => "",
        "companyname" => "",
        "mehiron_number" => "",
        "metria" => "",
        "underagent" => "",
        "metriaAmla" => "", 
        "sidx" => "person.person_number",
        "sord" => "asc",
        "rows" => "20",
        "page" => "1"
    );
    
        
					
 	public $agentFieldNames = array("person_id","firstname","lastname","email","city","address","telephone","fax","cellphone", "pass_word", "companyname", "mehiron_number", "metria", "underagent");
	public $editFieldNames = array("person_number", "pass_word", "companyname", "metria", "underagent","metriaAmla");									
	
        
        function __construct() 
        { 
            foreach($this->agentFields as $fieldName => $value) 
                $this->agentFields[(string)$fieldName]=( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : $this->agentFields[(string)$fieldName]);	

        } 
        
        public function  viewAgents()
	{
            
                   $this->agentFields["sidx"] = "person_number" ? "person.person_number"  : $this->agentFields["sidx"];
            
		   $filter_query_limit="SELECT * FROM person,agent WHERE ";   
                   $filter_query="SELECT COUNT(*) AS NumberOfRecords FROM person,agent WHERE  ";  
                   
                   $filter_query_body = "person.person_number LIKE '%".$this->agentFields["person_number"]."%' AND ";
                   $filter_query_body .= "person_id LIKE '%".$this->agentFields["person_id"]."%' AND ";
                   $filter_query_body .= "firstname LIKE '%".$this->agentFields["firstname"]."%' AND ";
                   $filter_query_body .= "lastname LIKE '%".$this->agentFields["lastname"]."%' AND ";
                   $filter_query_body .= "email LIKE '%".$this->agentFields["email"]."%' AND ";
                   $filter_query_body .= "city LIKE '%".$this->agentFields["city"]."%' AND ";
                   $filter_query_body .= "address LIKE '%".$this->agentFields["address"]."%' AND ";
                   $filter_query_body .= "telephone LIKE '%".$this->agentFields["telephone"]."%' AND ";
                   $filter_query_body .= "fax LIKE '%".$this->agentFields["fax"]."%' AND ";
                   $filter_query_body .= "cellphone LIKE '%".$this->agentFields["cellphone"]."%' AND ";
                   $filter_query_body .= "pass_word LIKE '%".$this->agentFields["pass_word"]."%' AND ";
                   $filter_query_body .= "companyname LIKE '%".$this->agentFields["companyname"]."%' AND ";
                   $filter_query_body .= "mehiron_number LIKE '%".$this->agentFields["mehiron_number"]."%' AND ";
                   $filter_query_body .= "metria LIKE '%".$this->agentFields["metria"]."%' AND ";
                   $filter_query_body .= "underagent LIKE '%".$this->agentFields["underagent"]."%' AND ";
                   $filter_query_body .= "metriaAmla LIKE '%".$this->agentFields["metriaAmla"]."%' AND ";
                   $filter_query_body .= "person.person_number=agent.person_number ";
                   
                   $filter_query_body .= "ORDER BY " .$this->agentFields["sidx"]." ".$this->agentFields["sord"]  ;
                   
                   $filter_query .=$filter_query_body;
                       
                   $filter_query_limit .=$filter_query_body. " LIMIT " .(($this->agentFields["page"]-1)*$this->agentFields["rows"])." , " .(($this->agentFields["page"])*$this->agentFields["rows"]);

                   //echo $filter_query_limit;


                   #----- get data from DB ------#
                   require("../includes/openConnection.php"); 
                   $filter_query_result = mysql_query($filter_query) or die(mysql_error());
                   $filter_query_limit_result = mysql_query($filter_query_limit) or die(mysql_error());
                   require("../includes/closeConnection.php");
                   #----- get data from DB ------#
		
                
                   #----- return json ------#
                   $jsonData = array();
                   $rows = array();
                   while($r1 = mysql_fetch_assoc($filter_query_limit_result)) 
                            $rows[] = $r1;


                   $r2 = mysql_fetch_assoc($filter_query_result);
                   $jsonData['records'] = $r2['NumberOfRecords'];
                   $jsonData['total']= ceil($jsonData['records']/$this->agentFields["rows"]);
                   $jsonData['page']=$this->agentFields["page"];
                   $jsonData['rows']=$rows;

                   print json_encode($jsonData);
		
	}
	
	public function  getMetriaAgent()
	{
		#----- view and filter sql query generator  ------#
		$sql_query="SELECT companyname,person_number FROM agent WHERE metria=1";
		//echo $sql_query;
		#----- view and filter sql query generator  ------#
		
		#----- get data from DB ------#
		require("../includes/openConnection.php"); 
		$q = mysql_query($sql_query) or die(mysql_error());
		require("../includes/closeConnection.php");
		#----- get data from DB ------#
		
		#----- return json ------#
		$rows = array();
		//$r['boss']="לא תחת אף אחד";
		while($r = mysql_fetch_assoc($q)) 
			{	
			$r[$r['person_number']]=$r['companyname'];
			 unset($r['person_number']);
			 unset($r['companyname']);
			$rows[] = $r;
			}
		print json_encode($rows);
		#----- return json ------#
		
	}
	
	public function  editAgent()
	{
		$person = new person();
		$person->editPerson();
		#----- update sql query generator  ------#
                $sql_query="UPDATE  agent SET  `pass_word` ='".$this->agentFields["pass_word"]."',
                `companyname` ='".$this->agentFields["companyname"]."',
                `mehiron_number` ='".$this->agentFields["mehiron_number"]."',
                `metria` ='".$this->agentFields["metria"]."',
                `underagent` ='".$this->agentFields["underagent"]."',
                `metriaAmla` ='".$this->agentFields["metriaAmla"]."' WHERE  person_number ='".$this->agentFields["person_number"]."';";
                //echo $sql_query;
		#----- update sql query generator  ------#
		
		#----- update data in DB ------#
		require("../includes/openConnection.php");
		if(mysql_query($sql_query)) echo "ok";
		require("../includes/closeConnection.php");
		#----- update data in DB ------#
		
	}
	
	public function  addAgent()
	{
		
		$person = new person();
		foreach ($this->editFieldNames as $fieldName) 
		${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");
		
		#----- insert data to DB ------#
		$person_number=$person->addPerson();
                
		require("../includes/openConnection.php");  
			if(mysql_query("INSERT INTO agent (`person_number`, `pass_word`, `companyname`, `mehiron_number`, `metria`, `underagent`,`metriaAmla`) VALUES ('{$person_number}', '{$pass_word}', '{$companyname}', '1', '{$metria}', '{$underagent}', '{$metriaAmla}');")) echo "ok";
			else echo mysql_error();
		require("../includes/closeConnection.php");
	//	}
		#----- insert data to DB ------#	
	}
	
	public function  deleteAgent()
	{
		$person = new person();
		$person->deletePerson();
		#----- delete data to DB ------#
		require("../includes/openConnection.php");  
		if(mysql_query("DELETE FROM agent WHERE person_number='".$this->agentFields["person_number"]."';")) echo "ok";
		require("../includes/closeConnection.php");
		#----- delete data to DB ------#	
			
	}

}


?>